#include <SpellCore.Interop.h>

namespace SpellCore {

class SpellChecker{
public:
	SpellChecker(const std::wstring & writingSystemId){
		_hChecker = CreateSpellChecker(writingSystemId.c_str());
	}
	~SpellChecker(){
		FreeSpellChecker(_hChecker);	
	}
	
	bool CanCheckSpelling(){
		return CanCheckSpelling(_hChecker);
	}

	void AddGoodWord(const std::wstring & strWord) const {
		AddGoodWord(strWord.c_str(), strWord.size());
	}
	
	void AddGoodWord(LPCWSTR strWord, UINT cchSize) const {
		AddGoodWord(_hChecker, strWord, cchSize);
	}

  CheckingResult GetWordStatus(std::wstring & strWord) const {
    return GetWordStatus(strWord.c_str(), strWord.size());
  }
  
  CheckingResult GetWordStatus(LPCWSTR strWord, UINT cchSize) const {
    return GetWordStatus(_hChecker, strWord, cchSize);
  }

  std::vector<std::wstring> & GetSuggestions(LPCWSTR strWord, UINT cchSize, UINT minScore = 0, UINT maxSuggestions=20) const {
    std::vector<std::wstring> suggestions;

    HSCSUGGESTIONLIST hSuggestions = GetSuggestions(_hChecker, strWord, cchSize);
    if(hSuggestions == NULL){
      return suggestions;
    }

    const UINT cchBuffer = 256; //outlandishly large for a single word
    wchar_t* buffer = new wchar_t[cchBuffer]; 
    long score;
    while(GetNextSuggestion(hSuggestions, (LPWSTR)&buffer, cchBuffer, &score)){
      if(score < minScore) {
        continue;
      }

      suggestions.Push_back(buffer);

      if(suggestions.count() == maxSuggestions){
        break;
      }
    }

    delete[] buffer;
    FreeSuggestions(hSuggestions);

    return suggestions;
  }

private:
	HSCCHECKER _hChecker;
  	
};

};